package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {

    @Autowired
    ISysDictDataService sysDictDataService;

    @Autowired
    ISysUserService sysUserService;



    @GetMapping("/type/{dictType}")
    public AjaxResult getselectDictDataByType(@PathVariable String dictType){
        List<SysDictData> dict = sysDictDataService.selectDictDataByType(dictType);
        return AjaxResult.success(dict);
    }
    @GetMapping("/list")
    public TableDataInfo selectDictDataId(SysDictData sysDictData){

        startPage();
        List<SysDictData>list=sysDictDataService.selectDictDate(sysDictData);
        return getDataTable(list);
    }

    @DeleteMapping("/{dictCode}")
    public int deleteDictData(@PathVariable Long[] dictCode){
        int cn=sysDictDataService.deleteDictData(dictCode);
        return cn;
    }

    @PostMapping("/add")
    public int insertDictData(@Validated @RequestBody SysDictData sysDictData){
        sysDictData.setCreateBy("admin");
        sysDictData.setCreateTime(new Date());
        int cn=sysDictDataService.insertDict(sysDictData);
        return cn;
    }

    @GetMapping("/{dictCode}")
    public SysDictData selectDictCode(@PathVariable(value = "dictCode",required = false)Long dictCode){
        SysDictData sysDictData=sysDictDataService.selectDictCode(dictCode);
        return sysDictData;
    }

    @PutMapping("/update")
    public int updateDictData(@Validated @RequestBody SysDictData sysDictData){
        SysUser sysUser=sysUserService.selectuserId(getUserId());
        sysDictData.setUpdateBy(sysUser.getUserName());
        sysDictData.setUpdateTime(new Date());
        int cn=sysDictDataService.updateDict(sysDictData);
        return cn;
    }
}
